clear all 
set more off 
set maxvar 15000 
clear matrix


    foreach zed in logs levels {
        
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
        keep if baseline_sample==1

        gen early = decade==1940 
        gen late =  decade==1950 
        keep if early==1 | late==1
        
    * KEY DIFFERENCE IN LOOP 
        if "`zed'"=="levels" {
            replace log_son_baseline = exp(log_son_baseline)
            replace log_father_baseline = exp(log_father_baseline)
        }
            
                   
        tempfile fulldata
        save `fulldata'

            
    * Run binscatters by period; save points
            
        foreach name in early late { 
            binscatter log_son_baseline log_father_baseline if `name'==1 [aw=wgt_sex_race], nq(10) savedata("binscatter_ige_all_`name'") replace
        } 

        foreach measure in ige {
            foreach name in early late { 
                insheet using "binscatter_`measure'_all_`name'.csv", clear 
                gen period="`name'"
                gen measure ="`measure'"
                save "binscatter_`measure'_all_`name'.dta", replace 
            } 
        }
        
    * Append
        use  "binscatter_ige_all_early", clear
        append using "binscatter_ige_all_late"
        
    * Run the regressions on the full panel; save constant and slope 
        gen slope=.
        gen intercept=.

        foreach measure in ige  {
            foreach name in early late { 
                preserve
                use `fulldata', clear
                
                keep if `name'==1
                
                reg log_son_baseline log_father_baseline [aw=wgt_sex_race]
                    
                restore
                        
                replace slope = _b[log_father_baseline] if period=="`name'" & measure=="ige"
                replace intercept = _b[_cons] if period=="`name'" & measure=="ige"    
            } 
        }   
        
    * Generate predicted values for each point using slope and intercept
            gen yhat_line = intercept + (slope*log_father_baseline) if measure=="ige"

    * Figures 
        foreach name in early late {
    
            sum slope if period=="`name'" & measure=="ige"
                local s`name' =`r(mean)'
                local slope`name': display %-04.2fc `s`name''   
        
            sum intercept if period=="`name'" & measure=="ige"
                local int`name' =`r(mean)'
                local intercept`name': display %-04.2fc `int`name'' 
        }

        if "`zed'"=="logs" {
        #delimit ;
            twoway (scatter log_son_baseline log_father_baseline if period=="early" & measure=="ige", m(oh) mc(blue*1.1)) 
                   (line yhat_line log_father_baseline if period=="early" & measure=="ige", lp(dash) lc(blue*1.1) lwidth(0.25))  
                   (scatter log_son_baseline log_father_baseline if period=="late" & measure=="ige", m(+) mc(orange_red)) 
                   (line yhat_line log_father_baseline if period=="late" & measure=="ige", lp(dash) lc(orange_red) lwidth(0.25)),
               
            legend(on rows(2) order(1 "1940-1949 Cohorts (Slope=`slopeearly')" 3 "1950-1959 Cohorts (Slope=`slopelate')") ring(0) pos(11))  
            xti(" " "Predicted parental logged income") yti("Respondent logged income" " ")  ; 
        #delimit cr
        graph export "$Mydirectory2/appendix_d/IGE_binscatter_1940_vs_1950_`zed'.pdf", as(pdf) replace 
        }
        
        if "`zed'"=="levels" {
        #delimit ;
            twoway (scatter log_son_baseline log_father_baseline if period=="early" & measure=="ige", m(oh) mc(blue*1.1)) 
                   (line yhat_line log_father_baseline if period=="early" & measure=="ige", lp(dash) lc(blue*1.1) lwidth(0.25))  
                   (scatter log_son_baseline log_father_baseline if period=="late" & measure=="ige", m(+) mc(orange_red)) 
                   (line yhat_line log_father_baseline if period=="late" & measure=="ige", lp(dash) lc(orange_red) lwidth(0.25)),
               
            legend(on rows(2) order(1 "1940-1949 Cohorts (Slope=`slopeearly')" 3 "1950-1959 Cohorts (Slope=`slopelate')") ring(0) pos(5))  
            xti(" " "Predicted parental income") yti("Respondent income" " ")  ; 
        #delimit cr
        graph export "$Mydirectory2/appendix_d/IGE_binscatter_1940_vs_1950_`zed'.pdf", as(pdf) replace 
        }
        
    }

        foreach period in early late {
            cap rm "binscatter_ige_all_`period'.csv" 
            cap rm "binscatter_ige_all_`period'.do" 
            cap rm "binscatter_ige_all_`period'.dta" 
        }

    